package com.lzz.utils;

import com.lzz.shiro.MyShiroRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zhenzhen
 * @date 2018-12-20 下午8:00
 */
@Configuration
public class ShiroConfig {
    @Bean
    public MyShiroRealm myShiroRealm() {
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        // 设置加密算法和加密次数
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(1);

        myShiroRealm.setCredentialsMatcher(matcher);
        return myShiroRealm;
    }

    /**
     * SecurityMannger是shiro框架的核心控件。subject把认证提交给SecurityMannger，
     * SecurityManager调用Authenticator完成认证，认证的具体过程由Authenticator调用的Realm完成的
     * @return
     */
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(this.myShiroRealm());
        return securityManager;
    }

    /**
     * 设置授权用到的relm
     * @return
     */
//    @Bean
//    public ModularRealmAuthenticator modularRealmAuthenticator() {
//        ModularRealmAuthenticator modularRealmAuthenticator = new ModularRealmAuthenticator();
//        List<Realm> realms = new ArrayList<>();
//        realms.add(this.myShiroRealm());
//        modularRealmAuthenticator.setRealms(realms);
//        return modularRealmAuthenticator;
//    }

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 拦截器
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 配置不会被拦截的链接
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/student/**", "anon");
//        filterChainDefinitionMap.put("/user/**", "authc");
//        filterChainDefinitionMap.put("/role/**", "anon");
        filterChainDefinitionMap.put("/permission/**", "anon");
        filterChainDefinitionMap.put("/user/login", "anon");
        // 配置退出过滤器，其中的具体的退出diamanteshiro已经替我们实现了
        filterChainDefinitionMap.put("/logout", "logout");
        // authc: 所有url必须通过认证后才可以访问；anon：所有url都可以匿名访问
        filterChainDefinitionMap.put("/**", "authc");

        shiroFilterFactoryBean.setLoginUrl("/user/loginView");
        // 登录成功后跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/user/index");

        // 未授权界面
        shiroFilterFactoryBean.setUnauthorizedUrl("/403.html");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

    /**
     * 授权相关配置
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    /**
     * 配置shoir授权异常处理器
     * @return
     */
    @Bean
    public HandlerExceptionResolver resolver() {
        HandlerExceptionResolver handlerExceptionResolver = new HandleShiroUnAuthorizedExceptionResolver();
        return handlerExceptionResolver;
    }
}
